package com.capinfo.service.impl;

import com.capinfo.base.BaseEntity;
import com.capinfo.base.BaseMapper;
import com.capinfo.base.CurrentUser;
import com.capinfo.base.impl.BaseServiceImpl;
import com.capinfo.entity.CapService;
import com.capinfo.entity.CapServiceDocument;
import com.capinfo.entity.CapServiceKind;
import com.capinfo.mapper.CapServiceMapper;
import com.capinfo.service.CapServiceDocumentService;
import com.capinfo.service.CapServiceService;
import com.capinfo.util.CommonUtil;
import com.capinfo.util.JsonUtil;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: sunbenjin
 * @date: 2020/3/3
 **/
@Service
public class CapServiceServiceImpl extends BaseServiceImpl<CapService,String> implements CapServiceService {

    @Autowired
    CapServiceMapper capServiceMapper;

    @Autowired
    CapServiceDocumentService capServiceDocumentService;
    @Override
    public BaseMapper<CapService, String> getMappser() {
        return capServiceMapper;
    }

    @Override
    public List<CapService> selectListByPage(CapService record) {
        return capServiceMapper.selectListByPage(record);
    }

    @Override
    public int insert(CapService capService) {
        capService = super.addValue(capService,false);
        return capServiceMapper.insert(capService);
    }

    @Override
    public CapService selectByPrimaryKey(Object o){
        return capServiceMapper.selectByPrimaryKey(o);
    }

    @Override
    public JsonUtil  delById(String id,boolean flag) {
        JsonUtil j=new JsonUtil();

        try {
            CapService capService = selectByPrimaryKey(id);
            if (flag) {
                //逻辑
                capService.setDelFlag(BaseEntity.DEL_FLAG_DELETE);
                updateByPrimaryKeySelective(capService);
            } else {
                //物理
                capService.setDelFlag(BaseEntity.DEL_FLAG_DELETE);
                updateByPrimaryKeySelective(capService);
//                capServiceTypeMapper.delById(id);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        j.setMsg("删除成功");
        return j;
    }

    @Override
    public void batchUpdateDocument(CapService capService) {
        CapServiceDocument capServiceDocument = new CapServiceDocument();
        capServiceDocument.setTmpId(capService.getDocTmpId());
        String userId = CommonUtil.getUser().getId();
        List<CapServiceDocument> list = capServiceDocumentService.selectListByPage(capServiceDocument);
        if(CollectionUtils.isNotEmpty(list)){
            list.forEach((data)->{data.setServiceId(capService.getId());data.setUpdateBy(userId);});

            capServiceDocumentService.batchUpdate(list);
        }
    }
}
